package com.system.healthproject.controller;

import com.system.healthproject.annotation.RequireToken;
import com.system.healthproject.common.ApiResult;
import com.system.healthproject.model.admin.paramsbean.LoginParams;
import com.system.healthproject.model.admin.paramsbean.MenuParams;
import com.system.healthproject.model.admin.paramsbean.RoleAddMenusParams;
import com.system.healthproject.model.admin.paramsbean.RoleParams;
import com.system.healthproject.pojo.Menu;
import com.system.healthproject.pojo.Role;
import com.system.healthproject.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Api(value = "角色和菜单管理",tags = "后端-角色和菜单管理API")
@RestController
@RequestMapping("/roleMenu")
@Validated
public class RoleMenuController {

    @Autowired
    RoleService roleService;

    @ApiOperation(value = "后端获取所有角色(后端已调)",httpMethod = "get")
    @GetMapping(value = "/roleList",produces = "application/json;charset=utf-8")
    public ApiResult<List<Role>> getRoleList(){
        List<Role> roleList = roleService.getRoleList();
        return ApiResult.successWithData("成功",roleList);
    }

    @ApiOperation(value = "后端获取所有菜单(后端已调)",httpMethod = "get")
    @GetMapping(value ="/menuList",produces = "application/json;charset=utf-8")
    public ApiResult<List<Menu>> getMenuList(){
        List<Menu> allMenu = roleService.getAllMenu();
        return ApiResult.successWithData("成功",allMenu);
    }

    @ApiOperation(value = "根据角色获取菜单(后端已调)",httpMethod = "get")
    @GetMapping(value ="/getMenuListByRole",produces = "application/json;charset=utf-8")
    public ApiResult<List<Menu>> getMenuListByRole(
            @NotNull Integer roleId){
        List<Menu> allMenu = roleService.getMenuByRole(roleId);
        return ApiResult.successWithData("成功",allMenu);
    }

    @ApiOperation(value = "后端删除菜单(后端已调)",response = ApiResult.class)
    @PostMapping(value = "/deleteMenuById",produces = "application/json;charset=utf-8")
    public ApiResult deleteMenuById(@NotNull Integer menuId){
        boolean b = roleService.deleteMenu(menuId);
        return ApiResult.withBoolean(b);
    }

    @ApiOperation(value = "后端删除角色(后端已调)",response = ApiResult.class)
    @PostMapping(value = "/deleteRoleById",produces = "application/json;charset=utf-8")
    public ApiResult deleteRoleById(
            @NotNull Integer roleId){
        ApiResult<Object> objectApiResult = roleService.deleteRoleById(roleId);
        return objectApiResult;
    }


    @ApiOperation(value = "后端添加角色(后端已调)",response = ApiResult.class)
    @PostMapping(value = "/addRole",produces = "application/json;charset=utf-8")
    public ApiResult addRole(
            @Validated
            @RequestBody RoleParams role){
        boolean b = roleService.addRole(role);
        return ApiResult.withBoolean(b);
    }

    @ApiOperation(value = "后端添加菜单(后端已调)",response = ApiResult.class)
    @PostMapping(value = "/addMenu",produces = "application/json;charset=utf-8")
    public ApiResult addMenu(
            @Validated
            @RequestBody MenuParams role){
        Boolean aBoolean = roleService.addMenu(role);
        return ApiResult.withBoolean(aBoolean);
    }

    @ApiOperation(value = "后端修改菜单(后端已调)",response = ApiResult.class)
    @PostMapping(value = "/changeMenu",produces = "application/json;charset=utf-8")
    public ApiResult changeMenu(
            @Validated @RequestBody
                    Menu menu){
        Boolean aBoolean = roleService.changeMenu(menu);
        return ApiResult.withBoolean(aBoolean);
    }

    @ApiOperation(value = "后端修改角色(后端已调)",response = ApiResult.class)
    @PostMapping(value = "/changRole",produces = "application/json;charset=utf-8")
    public ApiResult changRole(@Validated @RequestBody
                                           Role role){
        Boolean aBoolean = roleService.changRole(role);
        return ApiResult.withBoolean(aBoolean);
    }





    @ApiOperation(value = "用户修改角色(后端已调)",response = ApiResult.class)
    @PostMapping(value = "/userChangeRole",produces = "application/json;charset=utf-8")
    public ApiResult userChangeRole(Integer userId,Integer roleId){
        Boolean role = roleService.userChangeRole(userId, roleId);
        return ApiResult.withBoolean(role);
    }


    @ApiOperation(value = "角色批量删除菜单(后端已调)",response = ApiResult.class)
    @PostMapping(value = "/roleDeleteMenus",produces = "application/json;charset=utf-8")
    public ApiResult roleDeleteMenus(
            @RequestBody
            @ApiParam RoleAddMenusParams role){
        Boolean aBoolean = roleService.roleDeleteMenus(role);
        return ApiResult.withBoolean(aBoolean);
    }


    @ApiOperation(value = "角色批量添加菜单(后端已调)",response = ApiResult.class)
    @PostMapping(value = "/roleAddMenus",produces = "application/json;charset=utf-8")
    public ApiResult roleAddMenus(
            @RequestBody
            @ApiParam RoleAddMenusParams role){
        Boolean aBoolean = roleService.roleAddMenus(role);
        return ApiResult.withBoolean(aBoolean);
    }



}
